﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Cryptography_0912089_0912100
{
    public partial class DigitalSignatureForm : Form
    {
        AsymmetricCrytopher asymmetricCryptopher;

        public DigitalSignatureForm()
        {
            InitializeComponent();
        }

        #region Generate key pair

        private void buttonGenKeyPair_Click(object sender, EventArgs e)
        {
            if (textBoxPassword.Text == "")
            {
                MessageBox.Show("You must enter password to generate key pair", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (textBoxOutputKeyFile.Text == "")
            {
                MessageBox.Show("You must choose output of keypair file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            asymmetricCryptopher = new RSACryptopher(textBoxPassword.Text);
            asymmetricCryptopher.GeneratePrivateKey(textBoxPassword.Text, textBoxOutputKeyFile.Text);
            asymmetricCryptopher.GeneratePublicKey(textBoxPassword.Text, textBoxPublicKeyFile.Text);

            MessageBox.Show("Generate key pair file success", "Success", MessageBoxButtons.OK);
        }

        private void buttonBrowse_Click(object sender, EventArgs e)
        {
            DialogResult result = folderBrowserDialogGenKeyPair.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                textBoxOutputKeyFile.Text = folderBrowserDialogGenKeyPair.SelectedPath + "\\key.private";
                textBoxPublicKeyFile.Text = folderBrowserDialogGenKeyPair.SelectedPath + "\\key.public";
            }
        }
        #endregion

        #region Generate Digital Signature

        private void buttonBrowseKeypair_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDialogKeyPair.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                textBoxKeypair.Text = openFileDialogKeyPair.FileName;
            }
        }
        
        private void buttonBrowseFileData_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDialogFileData.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                textBoxFileData.Text = openFileDialogFileData.FileName;
            }

            if(textBoxOutputDigitalSignature.Text != @"")
            {
                string temp = textBoxOutputDigitalSignature.Text.Substring(0,textBoxOutputDigitalSignature.Text.LastIndexOf('\\'));
                string temp2 = textBoxFileData.Text.Substring(textBoxFileData.Text.LastIndexOf('\\')) + ".sign";
                textBoxOutputDigitalSignature.Text = temp + temp2;
            }
        }

        private void buttonBrowseOutputDigitalSignature_Click(object sender, EventArgs e)
        {
            if(textBoxFileData.Text == @"")
            {
                MessageBox.Show("Must choose file data", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            DialogResult result = folderBrowserDialogOutputDigitalSignature.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                textBoxOutputDigitalSignature.Text = folderBrowserDialogOutputDigitalSignature.SelectedPath +textBoxFileData.Text.Substring(textBoxFileData.Text.LastIndexOf('\\'))+ ".sign";
                
            }
        }

        private void buttonGenerateDigitalSignature_Click(object sender, EventArgs e)
        {
            if (textBoxKeypair.Text == "")
            {
                MessageBox.Show("You must choose file key pair to Sign", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (textBoxFileData.Text == "")
            {
                MessageBox.Show("You must choose file data to Sign", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (textBoxOutputDigitalSignature.Text == "")
            {
                MessageBox.Show("You must choose output file Digital Signature", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            asymmetricCryptopher = new RSACryptopher();
            string digitalSignature = asymmetricCryptopher.GenerateDigitalSignature(textBoxFileData.Text, textBoxKeypair.Text);

            StreamWriter streamWriter = new StreamWriter(textBoxOutputDigitalSignature.Text);
            streamWriter.Write(digitalSignature);
            streamWriter.Flush();
            streamWriter.Close();

            MessageBox.Show("Generate Digital Signature File success", "Success", MessageBoxButtons.OK);
        }

        #endregion

        #region Verify Digital Signature
        private void buttonKeyPairVerify_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDialogKeypairVerify.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                textBoxKeyPairVerify.Text = openFileDialogKeypairVerify.FileName;
            }
        }

        private void buttonFileDataVerify_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDialogFileDataVerify.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                textBoxFileDataVerify.Text = openFileDialogFileDataVerify.FileName;
            }
        }

        private void buttonFileDigitalSignatureVerify_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDialogFileDigitalSignatureVerify.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                textBoxFileDigitalSignatureVerify.Text = openFileDialogFileDigitalSignatureVerify.FileName;
            }
        }

        private void buttonVerify_Click(object sender, EventArgs e)
        {
            if (textBoxKeyPairVerify.Text == "")
            {
                MessageBox.Show("You must choose file key pair to Verify", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (textBoxFileDataVerify.Text == "")
            {
                MessageBox.Show("You must choose file data to Verify", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (textBoxFileDigitalSignatureVerify.Text == "")
            {
                MessageBox.Show("You must choose file Digital Signature to Verify", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            asymmetricCryptopher = new RSACryptopher();
            bool result = asymmetricCryptopher.VerifyDigitalSignature(textBoxFileDataVerify.Text, textBoxFileDigitalSignatureVerify.Text, textBoxKeyPairVerify.Text);
            if (result)
            {
                MessageBox.Show("The signature is match on file data", "Success", MessageBoxButtons.OK);
            }
            else
            {
                MessageBox.Show("The signature isn't match on file data", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
        #endregion
    }
}
